package physics;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.border.LineBorder;

/**
 * This class represents the gun object. This object can be imported
 * as part any JPanel and be used.
 * @author Krasimir Baylov(61080), Stanislav Petrov(61055)
 * @author 1.0
 */
public class GunPanel extends javax.swing.JPanel {

    /**
     * The values for the width and height of the arc on the panel
     */
    private static final int ARC_SIZE = 200;
    /**
     * The size of the panel (width = height = 200)
     */
    private static final int PANEL_SIZE = 200;
    /**
     * The angle that the gun makes with the X axis.
     * Will be presented in radians transperantly.
     * However a Degrees value will be given for set and get but
     * internally will hold a RADIAN value;
     */
    private double angle = 2;
    /**
     * The coordinates of the top of the gun.
     */
    private Point topCoordinates;

    /** Creates new form GunPanel */
    public GunPanel() {
        initComponents();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //draw the arc
        g.fillArc(-PANEL_SIZE / 2, PANEL_SIZE / 2, ARC_SIZE, ARC_SIZE, 0, 90);

        topCoordinates = new Point((int) ((PANEL_SIZE - 30) * Math.cos(angle)),
                (int) ((PANEL_SIZE - 30) * (1 - Math.sin(angle))));

        //draw the gun
        Graphics2D g2 = (Graphics2D) g;
        g2.setStroke(new BasicStroke(20));
        g2.drawLine(0, getHeight(), topCoordinates.x, topCoordinates.y);
    }

    /**
     * Getter for the top coordinates of the gun
     *
     * @return topCoordinates
     */
    public Point getTopCoordinates() {
        return topCoordinates;
    }

    /**
     * Set the angle of the gun and automatically
     * refresh the image to correspond to the updated
     * value of the angle.
     *
     * @param angleValue    the value that is set for the angle.
     *                  Internally presented in radians but degrees value is
     *                  added and automatic transformation is hold immedialety.
     */
    public void setAngle(double angleValue) {
        this.angle = Math.toRadians(angleValue);
        repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setPreferredSize(new java.awt.Dimension(200, 200));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 221, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 170, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
